package com.ztercelstudio.demo003

import android.util.Log
import androidx.compose.ui.text.intl.Locale
import androidx.compose.ui.text.toUpperCase


fun listUsage() {
    val list1 = listOf("A", "B", "C") // listOf
    list1.forEach{i-> Log.d("Demo003", "1. i = ${i}")}  //foreach

    val list2 = mutableListOf(1,3, 4, 6)  // mutableListOf
    list2.add(1)
    list2.add(100)          // add
    list2.addAll(listOf(4, 6, 0))   // addAll
    Log.d("Demo003", "2. list2 ${list2}")

    Log.d("Demo003", "3. list2[0] - ${list2.get(0)} list2[2] - ${list2[2]}")   // get []
    list2[2] = 999    // set []

    list2.add(3, 100)   // add index
    list2.addAll(10, listOf(1001, 1002))  // addAll index
    Log.d("Demo003", "4. list2 ${list2}")

    list2.replaceAll { if (it > 1000) it - 1000 else it }
    Log.d("Demo003", "5. list2 ${list2} ${list2.size}")


    list2.removeAt(2)
    Log.d("Demo003", "6. list2 ${list2} ${list2.size} max: ${list2.max()} min: ${list2.min()} sum: ${list2.sum()}")

    Log.d("Demo003", "7. list2 ${list2.filter { it < 100 }}")

    list2.sort()
    Log.d("Demo003", "8. list2 ${list2} ")

    Log.d("Demo003", "9. list2 ${list2.groupBy { it > 3 }} ")

    list2.removeIf({it < 5})
    Log.d("Demo003", "10. list2 ${list2} ${list2.size} }")

    Log.d("Demo003", "11. list2 ${list2.map { if (it < 10) it * 10 else it }} ")

    list2 += 10
    list2 += 20
    list2 += 30
    list2 += 40
    list2.forEachIndexed { index, i -> Log.d("Demo003", "list2[${index}] = ${i}") }
    Log.d("Demo003", "12. list2 ${list2}}  ${list2.contains(100)}")

    Log.d("Demo003", "13. list2 ${list2} ${list2.reduce{s, i -> s * i}}")

    val ll = list2.flatMap{ listOf(it) }
    Log.d("Demo003", "14. list2 ${ll}");
}

fun mapUsage() {
    val map1 = mapOf(11 to "zTercel", 21 to "yTercel", 31 to "keyboard")
    Log.d("Demo003", "1. map1 - ${map1} keys: ${map1.keys} values: ${map1.values}")

    val map2 = mutableMapOf(11 to "zTercel", 21 to "yTercel", 31 to "keyboard")
    map2[41] = "xiaobing"
    map2.set(21, "xTercel")
    Log.d("Demo003", "1. map2 - ${map2} keys: ${map2.keys} values: ${map2.values} 21 - ${map2.get(21)}")
}

fun setUsage() {
    val set1 = setOf(11 to "zTercel", 21 to "yTercel", 31 to "keyboard")
    Log.d("Demo003", "1. set1 - ${set1} ${set1.count()} list: ${set1.toList()} map: ${set1.toMap()}")

    val set2 = mutableSetOf(11, 11, 12, 14, 16, 17, 13, 14)
    Log.d("Demo003", "2. set2 - ${set2} +1 ==>  ${set2.map{k -> k+1}} ");
}

fun stringUsage() {
    var data = "hello, I am yiguigen   "
    Log.d("Demo003", "1. string ${data} len: ${data.length}")

    if (data.isNotEmpty() && data.contains("yiguigen")) {
        val restring = data.replace("yiguigen", "zTercel");
        val subString = data.substring(0, 5);
        val trimString = data.trim();
        Log.d("Demo003", "2. string =>[${data}] replace: ${restring} len: ${restring.length} sub: ${subString} [${trimString}] ")
    }

    val data2 = "hello".toUpperCase(Locale.current).toList()
    Log.d("Demo003", "3. string ${data2}")
}

fun sampleEntry() {
    Log.d("Demo003", "---- List功能 -----")
    listUsage()

    Log.d("Demo003", "---- Map 功能 -----")
    mapUsage()

    Log.d("Demo003", "---- Set 功能 -----")
    setUsage()

    Log.d("Demo003", "---- String 功能 -----")
    stringUsage()
}